import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDao {
    public boolean login(String username,String password,String telephone){
        //1获得数据库的连接（加载数据库驱动，通过drivermanager获取数据库连接）
        Connection conn= ConnectionManager.getConnection();
        try {
            //2通过connection对象获取statement对象
            String strSQL="insert into t_user(username,password,telephone) values(?,?,?)";
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            pstmt.setString(3,telephone);

            //3使用statement执行SQL语句
            int rs=pstmt.executeUpdate();
            //4对增删改查结果的处理
            if(rs>0){
                return true;
            }else{
                return false;
            }
        }catch (SQLException e){
            System.out.println("数据库的234步产生的异常");
            e.printStackTrace();
        }finally {
            //5关闭数据库
            ConnectionManager.closeConnection(conn);
        }
        return false;
    }

    public boolean signIn(String username,String password){
        //1获得数据库的连接（加载数据库驱动，通过drivermanager获取数据库连接）
        // SELECT * FROM `student`.`t_user` WHERE `username` LIKE '%chenyu%'
        Connection conn= ConnectionManager.getConnection();
        try {
            //2通过connection对象获取statement对象
            String strSQL="SELECT * FROM `t_user` WHERE `username` LIKE ?";
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            pstmt.setString(1,username);

            ResultSet rs = pstmt.executeQuery();

            //4对增删改查结果的处理
            if (rs.next()) {
                String storedPassword = rs.getString("password");
                return storedPassword.equals(password);
            }
        }catch (SQLException e){
            System.out.println("数据库的查询产生的异常");
            e.printStackTrace();
        }finally {
            //5关闭数据库
            ConnectionManager.closeConnection(conn);
        }
        return false;
    }


    public static void main(String[] args) {
//        new  UserDao().login("chenyu","202212390606","16607512918");
        System.out.println(new  UserDao().signIn("chenyu","202212390606"));
    }
}
